package co.yixiang.modules.shop.service.impl;

import co.yixiang.modules.shop.domain.Bank;
import co.yixiang.utils.ValidationUtil;
import co.yixiang.utils.FileUtil;
import co.yixiang.modules.shop.repository.BankRepository;
import co.yixiang.modules.shop.service.BankService;
import co.yixiang.modules.shop.service.dto.BankDto;
import co.yixiang.modules.shop.service.dto.BankQueryCriteria;
import co.yixiang.modules.shop.service.mapper.BankMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import co.yixiang.utils.PageUtil;
import co.yixiang.utils.QueryHelp;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @author lijunze
* @date 2020-05-08
*/
@Service
//@CacheConfig(cacheNames = "bank")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class BankServiceImpl implements BankService {

    private final BankRepository bankRepository;

    private final BankMapper bankMapper;

    public BankServiceImpl(BankRepository bankRepository, BankMapper bankMapper) {
        this.bankRepository = bankRepository;
        this.bankMapper = bankMapper;
    }

    @Override
    //@Cacheable
    public Map<String,Object> queryAll(BankQueryCriteria criteria, Pageable pageable){
        Page<Bank> page = bankRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(bankMapper::toDto));
    }

    @Override
    //@Cacheable
    public List<BankDto> queryAll(BankQueryCriteria criteria){
        return bankMapper.toDto(bankRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    //@Cacheable(key = "#p0")
    public BankDto findById(Integer id) {
        Bank bank = bankRepository.findById(id).orElseGet(Bank::new);
        ValidationUtil.isNull(bank.getId(),"Bank","id",id);
        return bankMapper.toDto(bank);
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public BankDto create(Bank resources) {
        resources.setCreateTime(LocalDateTime.now());
        resources.setDelFlag(0);
        return bankMapper.toDto(bankRepository.save(resources));
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public void update(Bank resources) {
        Bank bank = bankRepository.findById(resources.getId()).orElseGet(Bank::new);
        ValidationUtil.isNull( bank.getId(),"Bank","id",resources.getId());
        bank.copy(resources);
        bankRepository.save(bank);
    }

    @Override
    //@CacheEvict(allEntries = true)
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            bankRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<BankDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (BankDto bank : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("标题", bank.getTitle());
            map.put("联系电话", bank.getPhone());
            map.put("图片", bank.getImage());
            map.put("利息", bank.getLx());
            map.put("商户id", bank.getMerId());
            map.put("创建时间", bank.getCreateTime());
            map.put("删除标识", bank.getDelFlag());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}